home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / domacnost a kancelar / joomla / Joomla_1.5.4-Stable-Full_Package.exe / plugins / user / joomla.php < prev    next >
PHP Script  |  2008-07-06  |  7KB  |  230 lines

  1. <?php
  2. /**
  3. * @version        $Id: joomla.php 10381 2008-06-01 03:35:53Z pasamio $
  4. * @package        Joomla
  5. * @subpackage    JFramework
  6. * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license        GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14.  
  15. // Check to ensure this file is included in Joomla!
  16. defined('_JEXEC') or die( 'Restricted access' );
  17.  
  18. jimport('joomla.plugin.plugin');
  19.  
  20. /**
  21.  * Joomla User plugin
  22.  *
  23.  * @author        Johan Janssens  <johan.janssens@joomla.org>
  24.  * @package        Joomla
  25.  * @subpackage    JFramework
  26.  * @since         1.5
  27.  */
  28. class plgUserJoomla extends JPlugin
  29. {
  30.     /**
  31.      * Constructor
  32.      *
  33.      * For php4 compatability we must not use the __constructor as a constructor for plugins
  34.      * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
  35.      * This causes problems with cross-referencing necessary for the observer design pattern.
  36.      *
  37.      * @param     object $subject The object to observe
  38.      * @param     array  $config  An array that holds the plugin configuration
  39.      * @since 1.5
  40.      */
  41.     function plgUserJoomla(& $subject, $config) {
  42.         parent::__construct($subject, $config);
  43.     }
  44.  
  45.     /**
  46.      * Remove all sessions for the user name
  47.      *
  48.      * Method is called after user data is deleted from the database
  49.      *
  50.      * @param     array          holds the user data
  51.      * @param    boolean        true if user was succesfully stored in the database
  52.      * @param    string        message
  53.      */
  54.     function onAfterDeleteUser($user, $succes, $msg)
  55.     {
  56.         if(!$succes) {
  57.             return false;
  58.         }
  59.  
  60.         $db =& JFactory::getDBO();
  61.         $db->setQuery('DELETE FROM #__session WHERE userid = '.$db->Quote($user['id']));
  62.         $db->Query();
  63.  
  64.         return true;
  65.     }
  66.  
  67.     /**
  68.      * This method should handle any login logic and report back to the subject
  69.      *
  70.      * @access    public
  71.      * @param   array   holds the user data
  72.      * @param     array   array holding options (remember, autoregister, group)
  73.      * @return    boolean    True on success
  74.      * @since    1.5
  75.      */
  76.     function onLoginUser($user, $options = array())
  77.     {
  78.         jimport('joomla.user.helper');
  79.  
  80.         $instance =& $this->_getUser($user, $options);
  81.  
  82.         // if _getUser returned an error, then pass it back.
  83.         if (JError::isError( $instance )) {
  84.             return $instance;
  85.         }
  86.  
  87.         // If the user is blocked, redirect with an error
  88.         if ($instance->get('block') == 1) {
  89.             return JError::raiseWarning('SOME_ERROR_CODE', JText::_('E_NOLOGIN_BLOCKED'));
  90.         }
  91.  
  92.         // Get an ACL object
  93.         $acl =& JFactory::getACL();
  94.  
  95.         // Get the user group from the ACL
  96.         if ($instance->get('tmp_user') == 1) {
  97.             $grp = new JObject;
  98.             // This should be configurable at some point
  99.             $grp->set('name', 'Registered');
  100.         } else {
  101.             $grp = $acl->getAroGroup($instance->get('id'));
  102.         }
  103.  
  104.         //Authorise the user based on the group information
  105.         if(!isset($options['group'])) {
  106.             $options['group'] = 'USERS';
  107.         }
  108.  
  109.         if(!$acl->is_group_child_of( $grp->name, $options['group'])) {
  110.             return JError::raiseWarning('SOME_ERROR_CODE', JText::_('E_NOLOGIN_ACCESS'));
  111.         }
  112.  
  113.         //Mark the user as logged in
  114.         $instance->set( 'guest', 0);
  115.         $instance->set('aid', 1);
  116.  
  117.         // Fudge Authors, Editors, Publishers and Super Administrators into the special access group
  118.         if ($acl->is_group_child_of($grp->name, 'Registered')      ||
  119.             $acl->is_group_child_of($grp->name, 'Public Backend'))    {
  120.             $instance->set('aid', 2);
  121.         }
  122.  
  123.         //Set the usertype based on the ACL group name
  124.         $instance->set('usertype', $grp->name);
  125.  
  126.         // Register the needed session variables
  127.         $session =& JFactory::getSession();
  128.         $session->set('user', $instance);
  129.  
  130.         // Get the session object
  131.         $table = & JTable::getInstance('session');
  132.         $table->load( $session->getId() );
  133.  
  134.         $table->guest         = $instance->get('guest');
  135.         $table->username     = $instance->get('username');
  136.         $table->userid         = intval($instance->get('id'));
  137.         $table->usertype     = $instance->get('usertype');
  138.         $table->gid         = intval($instance->get('gid'));
  139.  
  140.         $table->update();
  141.  
  142.         // Hit the user last visit field
  143.         $instance->setLastVisit();
  144.  
  145.         return true;
  146.     }
  147.  
  148.     /**
  149.      * This method should handle any logout logic and report back to the subject
  150.      *
  151.      * @access public
  152.      * @param  array    holds the user data
  153.      * @param     array   array holding options (client, ...)
  154.      * @return object   True on success
  155.      * @since 1.5
  156.      */
  157.     function onLogoutUser($user, $options = array())
  158.     {
  159.         //Make sure we're a valid user first
  160.         if($user['id'] == 0) return true;
  161.  
  162.         $my =& JFactory::getUser();
  163.         //Check to see if we're deleting the current session
  164.         if($my->get('id') == $user['id'])
  165.         {
  166.             // Hit the user last visit field
  167.             $my->setLastVisit();
  168.  
  169.             // Destroy the php session for this user
  170.             $session =& JFactory::getSession();
  171.             $session->destroy();
  172.         } else {
  173.             // Force logout all users with that userid
  174.             $table = & JTable::getInstance('session');
  175.             $table->destroy($user['id'], $options['clientid']);
  176.         }
  177.         return true;
  178.     }
  179.  
  180.     /**
  181.      * This method will return a user object
  182.      *
  183.      * If options['autoregister'] is true, if the user doesn't exist yet he will be created
  184.      *
  185.      * @access    public
  186.      * @param   array   holds the user data
  187.      * @param     array   array holding options (remember, autoregister, group)
  188.      * @return    object    A JUser object
  189.      * @since    1.5
  190.      */
  191.     function &_getUser($user, $options = array())
  192.     {
  193.         $instance = new JUser();
  194.         if($id = intval(JUserHelper::getUserId($user['username'])))  {
  195.             $instance->load($id);
  196.             return $instance;
  197.         }
  198.  
  199.         //TODO : move this out of the plugin
  200.         jimport('joomla.application.component.helper');
  201.         $config   = &JComponentHelper::getParams( 'com_users' );
  202.         $usertype = $config->get( 'new_usertype', 'Registered' );
  203.  
  204.         $acl =& JFactory::getACL();
  205.  
  206.         $instance->set( 'id'            , 0 );
  207.         $instance->set( 'name'            , $user['fullname'] );
  208.         $instance->set( 'username'        , $user['username'] );
  209.         $instance->set( 'password_clear'    , $user['password_clear'] );
  210.         $instance->set( 'email'            , $user['email'] );    // Result should contain an email (check)
  211.         $instance->set( 'gid'            , $acl->get_group_id( '', $usertype));
  212.         $instance->set( 'usertype'        , $usertype );
  213.  
  214.         //If autoregister is set let's register the user
  215.         $autoregister = isset($options['autoregister']) ? $options['autoregister'] :  $this->params->get('autoregister', 1);
  216.  
  217.         if($autoregister)
  218.         {
  219.             if(!$instance->save()) {
  220.                 return JError::raiseWarning('SOME_ERROR_CODE', $instance->getError());
  221.             }
  222.         } else {
  223.             // No existing user and autoregister off, this is a temporary user.
  224.             $instance->set( 'tmp_user', true );
  225.         }
  226.  
  227.         return $instance;
  228.     }
  229. }
  230.